import timber.controller.*;
import timber.ui.LoadView;


/**
	Responds to navigation events from App to create and load pages in the flash framework.
	@implementationNote Requires App and Page.
	@see App.
	@see Page.
	
	@author Chandler McWilliams
	@version 2005-08-04
*/
dynamic class timber.controller.PageFactory extends LoadView
{
	
	private var page = false;
	private var current = false;
	private var next = false;
	private static var instance = null;
	
// ===========================================================
// - CONSTRUCTOR
// ===========================================================
	public function PageFactory() {
		// add app listener and delegate
		App.addListener(this);
		App.addTransitionDelegate(this);
		
		if (instance == null) {
			instance = this;
		} else {
			// TODO: Should Throw
			trace("More than one instance of PageFactory found, only the first is available via the Singleton's getPage() method.");
		}
	}	
	

// ===========================================================
// - HANDLE NAVIGATION EVENTS
// ===========================================================
	// when navigation happens,
	// make sure there is a page ready for the event	
	public function onNavigate(path) {
		if (current == path[0]) {
			//onLoadInit(this.page);
			if (page.didRender()) {
				this.page.executeSubSection(App.getLocation()[1]);
			} else {
				onLoadInit(this.page);
			}
			return;
		}
		
		// save it
		current = path[0];
		
		// load the swf
		var swf = _global[path[0].toUpperCase()];
		if (!swf) {
			// TODO: Throw an error
			//App.error('PageFactory could not find a swf for: ' + path[0] +'.'+newline+'Make sure the class has been given a swf in config.xml.');
			//return false;
			load(path[0]+'.swf');
		} else {
			load(swf);
		}
	}


// ===========================================================
// - LOAD A SECTION
// ===========================================================
	public function load(theurl) {
		// if there is no img_mc, create one
		if (!page) {
			this.createEmptyMovieClip('page', 1);
			Stage.addListener(page);
		}

		// clear old
		this.unload();
		
		// load new
		loader.loadClip(theurl, this.page);
	}


// ===========================================================
// - WHEN THE LOADING IS DONE
// ===========================================================
	public function onLoadInit(clip) {
		// make the page into the requested class
		var cname = App.getLocation()[0].ucfirst();
		var p = new _global[cname]();
		
		// if there isn't an XML id for the object, set to the class name + xml
		if (!p.getXMLID()) p.setXMLID(cname.toLowerCase() + 'xml');
				
		if (!p) {
			// TODO: Throw
			App.error('Class: '+cname+' not found.'+newline+'Make sure you have listed the class in main.as.');
			return false;
		} else {
			p.init(page, App.getLocation()[1]);
		}
	}


// ===========================================================
// - APP DELEGATE FOR TRANSITIONS
// ===========================================================
	public function requiresOutTransition(newpath) {
		if (this.page) {
			return this.page.requiresOutTransition(newpath);
		}
		return false;
	}
	
	public function onOutTransition(oldpath, newpath) {
		return this.page.outTransition(oldpath, newpath);
	}
	


// ===========================================================
// - SINGLETON ACCESSORS
// ===========================================================
	// returns the current page. If there is more than instance
	// this will return the page for the first one. 
	public static function getPage() {
		return instance.page;
	}

}

